package com.taotao.manage.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.taotao.common.bean.EasyUIResult;
import com.taotao.manage.pojo.Item;
import com.taotao.manage.pojo.ItemCat;
import com.taotao.manage.pojo.ItemDesc;
import com.taotao.manage.service.ItemCatService;
import com.taotao.manage.service.ItemDescService;
import com.taotao.manage.service.ItemService;

/**
 * 商品模块
 * 
 * @author Beck
 *
 */
@Controller
@RequestMapping("item")
public class ItemController {
	@Autowired
	private ItemService itemService;

	@Autowired
	private ItemDescService itemDescService;

	@Autowired
	private ItemCatService itemCatService;

	/**
	 * 修改商品 参数: Item 商品对象，desc 商品描述 返回值： 无
	 */
	@RequestMapping(method = RequestMethod.PUT)
	public ResponseEntity<Void> updateItemAndItemDesc(Item item, @RequestParam("desc") String desc) {

		try {
			if (item == null) {
				// 返回 400
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			if (item.getId() == null || item.getCid() == null) {
				// 返回 400
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			// 更新 需要放在同一个事务中
			// 查询商品分类的名称
			ItemCat itemCat = itemCatService.queryById(item.getCid());
			item.setCname(itemCat.getName());
			itemService.updateItemAndItemDesc(item, desc);
			// 返回 204
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 返回500
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}

	/**
	 * 新增商品
	 */
	@RequestMapping(method = RequestMethod.POST)
	public ResponseEntity<Void> save(Item item, @RequestParam("desc") String desc) {

		try {
			// 对传入的参数进行校验
			if (StringUtils.isBlank(item.getTitle()) || item.getPrice() == null) {
				// 返回 400
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			// 进行保存商品
			// 查询商品分类的名称
			ItemCat itemCat = itemCatService.queryById(item.getCid());
			item.setCname(itemCat.getName());
			itemService.saveItem(item, desc);
			// 保存成功 返回201
			return ResponseEntity.status(HttpStatus.CREATED).build();

		} catch (Exception e) {
			e.printStackTrace();
		}

		// 返回500
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}

	/**
	 * 分页查询商品信息 参数:page 当前页,rows 每页显示多少条 返回参数：total 一共多少条数据，List<Item> rows
	 * 当前页的所有数据
	 */
	@RequestMapping("list")
	public ResponseEntity<EasyUIResult> queryItemByPage(@RequestParam("page") int pageNum,
			@RequestParam("rows") Integer pageSize) {
		try {
			PageInfo<Item> pageInfo = itemService.queryPageListByWhere(null, pageNum, pageSize);
			// 200
			return ResponseEntity.status(HttpStatus.OK).body(new EasyUIResult(pageInfo.getTotal(), pageInfo.getList()));
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 500
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}

	/**
	 * 根据商品的id获取商品详情 参数: 商品的id 返回值: 商品详情对象
	 */
	@RequestMapping("desc/{itemId}")
	public ResponseEntity<ItemDesc> queryItemDescByItemId(@PathVariable("itemId") Long itemId) {
		try {
			ItemDesc record = new ItemDesc();
			record.setItemId(itemId);
			ItemDesc itemDesc = itemDescService.queryOne(record);

			// 200
			return ResponseEntity.status(HttpStatus.OK).body(itemDesc);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 500
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
}
